﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.Base.Util
{
    public class NumericHelper
    {
        private static string ZERO = "0000000000";

        private static string TrimStartZero(string strValue)
        {
            if (strValue.StartsWith("0") == false)
                return strValue;

            int iStartIndex = 0;
            strValue = strValue.Trim();
            char[] cValues = strValue.ToCharArray();
            while ((iStartIndex < strValue.Length) && (cValues[iStartIndex] == '0')) 
                iStartIndex++;
            return (iStartIndex >= strValue.Length) 
                    ? "0" 
                    : (iStartIndex == 0) 
                        ? strValue 
                        : strValue.Substring(iStartIndex - 1);            
        }

        public static int ToInt(string strValue)
        {
            return int.Parse(TrimStartZero(strValue));
        }

        public static float ToFloat(string strValue)
        {
            return (float)ToDouble(strValue);
        }

        public static double ToDouble(string strValue)
        {
            return double.Parse(TrimStartZero(strValue));
        }

        public static double Round(double dValue, int iDigit)
        {
            return iDigit == 0
                    ? double.Parse(dValue.ToString("0"))
                    : double.Parse(dValue.ToString("0." + ZERO.Substring(0, iDigit)));    
        }

        public static int HexToInt(string strHex)
        {
            return int.Parse(strHex, System.Globalization.NumberStyles.HexNumber);
        }

        public static string IntToHex(int iValue)
        {
            return iValue.ToString("X");
        }

        public static bool IsNumeric(object Expression)
        {
            // Variable to collect the Return value of the TryParse method.
            bool isNum;

            // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
            double retNum;

            // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
            // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
            isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;
        }

        public static int Randomize()
        {
            return Randomize(int.MinValue, int.MaxValue);
        }

        public static int Randomize(int iMaxValue)
        {
            return Randomize(int.MinValue, iMaxValue);
        }

        public static int Randomize(int iMinValue, int iMaxValue)
        {
            Random randomize = new Random(Guid.NewGuid().GetHashCode());

            return randomize.Next(iMinValue, iMaxValue);
        }

        public static double ParseDouble(string strValue)
        {
            try
            {
                return double.Parse(strValue);
            }
            catch (Exception ex)
            {
            }
            return double.NaN;
        }

    }
}
